package com.server.Service;

import com.server.Example.OrdersExample;
import com.server.entity.Orders;
import com.server.entity.Sessions;
import com.server.entity.Types;
import com.server.entity.Users;
import com.server.mapper.OrdersMapper;
import org.apache.ibatis.session.SqlSession;
import untils.MybatisClass;
import untils.Utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * The type Order service.
 *
 * @author user
 */
public class OrderService {
    /**
     * Select array list.
     *
     * @return the array list
     */
    public static ArrayList<Orders> select(){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        OrdersMapper orderMapper = sqlSession.getMapper(OrdersMapper.class);
        ArrayList<Orders> ordersArrayList = (ArrayList<Orders>) orderMapper.selectByExample(null);
        sqlSession.close();
        /*return JSON.toJSONString(moviesList);*/
        return ordersArrayList;
    }

    /**
     * Select array list.
     *
     * @param selective the selective
     * @return the array list
     */
    public static ArrayList<Orders> select(Boolean selective){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        OrdersMapper orderMapper = sqlSession.getMapper(OrdersMapper.class);
        OrdersExample orderExample = new OrdersExample();
        orderExample.createCriteria().andIsFullyEqualTo(selective);
        ArrayList<Orders> ordersArrayList = (ArrayList<Orders>) orderMapper.selectByExample(orderExample);
        sqlSession.close();
        /*return JSON.toJSONString(moviesList);*/
        return ordersArrayList;
    }

    /**
     * Select array list.
     *
     * @param selective the selective
     * @param Id        the id
     * @return the array list
     */
    public static ArrayList<Orders> select(Boolean selective, Integer Id){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        OrdersMapper orderMapper = sqlSession.getMapper(OrdersMapper.class);
        OrdersExample orderExample = new OrdersExample();
        orderExample.createCriteria().andIsFullyEqualTo(selective).andUserInfoEqualTo(Id);
        ArrayList<Orders> ordersArrayList = (ArrayList<Orders>) orderMapper.selectByExample(orderExample);
        sqlSession.close();
        /*return JSON.toJSONString(moviesList);*/
        return ordersArrayList;
    }

    /**
     * Select orders.
     *
     * @param ID the id
     * @return the orders
     */
    public static Orders select(Integer ID){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        OrdersMapper orderMapper = sqlSession.getMapper(OrdersMapper.class);
        Orders orders = orderMapper.selectByPrimaryKey(ID);
        sqlSession.close();
        return orders;
    }

    /**
     * Insert int.
     *
     * @param number    the number
     * @param startTime the start time
     * @param entire    the entire
     * @return the int
     */
    public static int insert(int number, String startTime, String entire){
        int sumCount = 0;
        SqlSession sqlSession=MybatisClass.getSqlSession();
        OrdersMapper moviesTypeMapper = sqlSession.getMapper(OrdersMapper.class);
        List<Users> users = UserService.getAllTypes();
        List<Types> types = TypesService.getAllTypes();
        List<Sessions> sessions = SessionsService.nextWeekMovies();
        for (int i = 0; i <number ; i++) {
            boolean isTrue= new Random().nextBoolean();
            sumCount+=moviesTypeMapper.insert(new Orders(
                    sessions.get(new Random().nextInt(users.size())).getSessionsId(),
                    users.get(new Random().nextInt(users.size())).getUserId(),
                    Utils.getRandomString(12)+i,
                    types.get(new Random().nextInt(users.size())).getTypeId(),
                    BigDecimal.valueOf(Math.random() * (100 - 10) + 10).setScale(2, RoundingMode.DOWN),
                    isTrue,
                    Utils.randomDate(startTime, entire),
                    isTrue?Utils.randomDate(startTime, entire):null
            ));
            sqlSession.commit();
        }
        sqlSession.close();
        return sumCount;
    }
}
